﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Configuration;
using System.Linq;
using System.Web;
using System.IO;
using OrmAuto;

/// <summary>
/// Descrizione di riepilogo per gestHandlerEngine
/// </summary>
public class gestHandlerEngine
{
		HttpContext mContext = null;

		public gestHandlerEngine()
		{
		}

		public void Execute(HttpContext context)
		{
				mContext = context;

				string azione = string.Empty;

				if (context.Request.QueryString.Count > 0)
						azione = context.Request.QueryString["azione"].ToString();

				if (context.Request.Form.Count > 0)
				{
				}

				if (context.Request.HttpMethod.Equals("POST", StringComparison.CurrentCultureIgnoreCase))
				{
						var input = context.Request.InputStream;
						using (System.IO.StreamReader sr = new StreamReader(input, System.Text.Encoding.UTF8))
						{
								string pageContent = sr.ReadToEnd();
								pageContent = context.Server.UrlDecode(pageContent);
								//setMessaggio(pageContent);    
						}
				}

				object result = null;

				var postdata = context.Request.Form.AllKeys.Length;

				switch (azione)
				{
						case "getAnnVendita":
								result = prelevaAnnunciVendita();
								break;
						default:
								result = string.Empty; break;
				}

				//CONTROLLA IL RISULTATO SERIALIZZATO JSON
				if (result != null)
				{
						//var ser = (new JSonner().SerializeToJsonString(result));
						var ser = new JSonner().SerializeToXml(result); 
						context.Response.Write(ser);
				}

				context.Response.End();

		}//fm

		public object prelevaAnnunciVendita()
		{
				var uid = mContext.Request.QueryString["uid"].ToString();
				int userId = 0;
				int.TryParse(uid, out userId);

				var dati = new OrmAuto.QuerierAnnunci().getAnnVendita(userId, 0);
				return dati;
				
				try
				{
						var a = (new JSonner().SerializeToJsonString(dati));
				}
				catch { }
				try
				{
						return (new JSonner().SerializeToXml(dati));
				}
				catch { }

   return null;

		}//fM


}//fC
